package com.cykj.handler;

import com.cykj.entity.GoodsEntity;
import com.cykj.entity.PageEntity;
import com.cykj.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsHandler {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/findGoods")
    public Map<String,Object> findGoods(@RequestParam(value = "name",defaultValue = "") String name,
                         @RequestParam(value = "id",defaultValue = "") String id,
                         @RequestParam(value = "pagecurr",defaultValue = "1")Integer pagecurr,
                         @RequestParam(value = "pagesize",defaultValue = "3")Integer pagesize){


        PageEntity pageEntity = new PageEntity();
        pageEntity.setPageCurrent(pagecurr);
        pageEntity.setPageSize(pagesize);
        pageEntity.computeStart();

        name = "%" + name + "%";
        Integer gid = null;
        if (!"".equals(id)){
            gid = Integer.parseInt(id);
        }

        List<GoodsEntity> list = goodsService.findGoods(name,gid,pageEntity);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("page",pageEntity);

        return map;
    }
}
